/**
 * @(#)JSONHandler.java
 *
 * Copyright 2011 naryou, Inc. All rights reserved.
 */
package com.dili.dd.gpi.util.spring;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Arrays;

/**
 * description
 * 
 * @author jianguo.xu
 * @version 1.0,2011-1-12
 */
public class JSONHandler {
	private static final Log LOG = LogFactory.getLog(JSONHandler.class);

    /**
     * 格式化JSON串时，默认使用的特性
     */
    private static final SerializerFeature[] DEFAULT_FEATURE = {
            SerializerFeature.WriteDateUseDateFormat
    };
	
	/**
	 * 根据 paramKey在jsonconfig.xml中得到object对象 的json配置<br/>
	 * 如果为找到对应的json配置则采用默认的方式解析object
	 * @author jianguo.xu
	 * @param object
	 * @return
	 */
	public static final String serialize(Object object, SerializerFeature ... options) {
        //合并默认特性和用户定义的特性
        SerializerFeature[] serializerFeatures = Arrays.copyOf(options, options.length + DEFAULT_FEATURE.length);
        System.arraycopy(DEFAULT_FEATURE,0,serializerFeatures,options.length,DEFAULT_FEATURE.length);
        try {
            return JSON.toJSONString(object,serializerFeatures);
        }catch (Exception e){
            LOG.error("parser json error.");
            throw new RuntimeException(e);
        }
    }

}